<template>
  <div class="bk">
    <img class="title_h1" src="/static/news/xw_bk.jpg" ref="imgH">
    <div v-if="isList" class="xw">
      <div class="newsblock" v-for="(item,index) in classList" :key="index">
        <div class="classtitle" v-if="!index %2">
          <div class="classtitle-item1"><img :src="index == 0?'/static/news/xw_syjc.jpg':'/static/news/xw_zxtj.jpg'" />
          </div>
        </div>
        <div class="articleblock" :style="index%2?'float:left':'float:right'">
          <ul v-if="index==newsList1ClassIndex" style="padding-left:20px">
            <li v-for="news in newsList1" :key="news.id" @click="enterDetails(item.id,news.id)">
              <span class="articledot"></span>
              <span class="articlename">{{news.title}}</span>
              <span class="articletime">{{news.addtime.substring(0,10)}}</span>
            </li>
          </ul>
          <ul v-if="index==newsList2ClassIndex">
            <li v-for="news in newsList2" :key="news.id" @click="enterDetails(item.id,news.id)">
              <span class="articledot"></span>
              <span class="articlename">{{news.title}}</span>
              <span class="articletime">{{news.addtime.substring(0,10)}}</span>
            </li>
          </ul>
        </div>
        <div class="classtitle" v-if="index%2" :style="index%2?'float:right':'float:right'">
          <div class="classtitle-item1"><img :src="index == 0?'/static/news/xw_syjc.jpg':'/static/news/xw_zxtj.jpg'" />
          </div>
        </div>
      </div>
    </div>
    <div v-if="!isList" class="xw">
      <div class="newsdetailsblock">
        <div class="leftnav">
          <div v-for="(item,index) in classList" :key="index">
            <div class="newsdetails-classtitle">
              <img :src="index != 1?'/static/news/xw_jcico.png':'/static/news/xw_zxtjico.png'"
                class="newsdetails-classtitle-item1" />
              <div class="newsdetails-classtitle-item2">{{item.title}}</div>
            </div>
            <ul v-if="index==newsList1ClassIndex">
              <li :class="{'activenews':(item.id==currentClassId&&news.id==currentNewsId)}"
                v-for="(news,newsindex) in newsList1" :key="newsindex" @click="selectedNews(item.id,news.id)">
                <span class="newsdot"></span>
                {{news.title}}
              </li>
            </ul>
            <ul v-if="index==newsList2ClassIndex">
              <li :class="{'activenews':(item.id==currentClassId&&news.id==currentNewsId)}"
                v-for="(news,newsindex) in newsList2" :key="newsindex" @click="selectedNews(item.id,news.id)">
                <span class="newsdot"></span>
                {{news.title}}
              </li>
            </ul>
          </div>
        </div>
        <div class="rightdtals">
          <div class="newsdetails-title">{{newsDetails.title}}</div>
          <div class="newsdetails-content" v-html="newsDetails.content"></div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import {
    forEach
  } from 'jszip';
  export default {
    data() {
      return {
        classList: [],
        newsList: [], //数组1
        newsList1: [],
        newsList1ClassIndex: 0, //数组的索引
        newsList1RowCount: 0, //总行数
        newsList1RowCountIndex: [], //索引集合
        newsList2: [],
        newsList2ClassIndex: 0,
        newsList2RowCount: 0,
        newsList2RowCountIndex: [],
        newsList3: [],
        newsList3ClassIndex: 0,
        newsList3RowCount: 0,
        newsList3RowCountIndex: [],
        totalCount: 0,
        currentPage: 1,
        pageSize: 10,
        isList: true, //true的时候显示列表，否则显示详情
        currentClassId: 0, //来回切换当前选中的类别
        currentNewsId: 0, //来回切换当前选中的文章
        newsDetails: {
          title: '',
          content: ''
        },
        hasLogin: false
      };
    },
    created: function () {
      //判断是否已经登录了
      var token_time = sessionStorage["token_time"];
      token_time = parseInt(token_time);
      token_time = token_time + 3600000;
      var now = new Date().getTime();
      if (token_time < now || token_time == undefined || isNaN(token_time)) {
        // console.log("未登录");
        this.hasLogin = false;
      } else {
        this.hasLogin = true;
      }

    },
    methods: {
      //获取类别列表
      getClassList() {
        let params = new URLSearchParams();
        params.append('pageNum', 1);
        params.append('pageSize', 100);
        this.$post(this.API.articleClassList, params).then(res => {
          // console.log("请求类别列表")
          // console.log(res)
          if (res.code === 1) {
            let list = res.data.list;
            // this.classList = list;
            for (let index = 0; index < list.length; index++) {
              //如果未登录，排除企业签名板块
              if (!this.hasLogin) {
                if (index !== 2) {
                  this.classList.push(list[index]);
                  this.getNewsList(this.classList[index].id, index);
                }
              } else {
                this.classList.push(list[index]);
                this.getNewsList(this.classList[index].id, index);
              }
            }
          }
        });
      },
      //获取新闻列表
      getNewsList(id, index) {
        let params = new URLSearchParams();
        params.append('pageNum', 1);
        params.append('pageSize', 10000);
        params.append('classes_id', id);
        this.$post(this.API.articleList, params).then(res => {
          if (res.code === 1) {
            let list = res.data.list;
            if (index == 0) {
              this.newsList1 = list;
              this.newsList1ClassIndex = index;
              this.newsList1RowCount = list.length;
            } else if (index == 1) {
              this.newsList2 = list;
              this.newsList2ClassIndex = index;
              this.newsList2RowCount = list.length;
            }
          }
        });
      },
      //获取新闻详情
      getNewsDetails(id) {
        // console.log(id);
        let params = new URLSearchParams();
        params.append('article_id', id);
        this.$post(this.API.articleDetail, params).then(res => {
          // console.log("请求新闻详情")
          // console.log(res)
          if (res.code === 1) {
            let info = res.data.info;
            this.newsDetails.title = info.title;
            this.newsDetails.content = info.content;
          }
        });
      },
      goNews(url) {
        let a = document.createElement("a");
        a.setAttribute("href", url);
        a.setAttribute("target", "blank");
        // console.log(a);
        document.body.appendChild(a);
        a.click();
      },
      currentPageChange(e) {
        this.currentPage = e;
        this.getNewsList();
      },
      enterDetails(classid, newsid) {
        console.log(classid + '!!!' + newsid)
        this.isList = !this.isList;
        this.currentClassId = classid;
        this.currentNewsId = newsid;
        this.getNewsDetails(newsid);
      },
      selectedNews(classid, newsid) {
        // console.log(classid + newsid)
        this.currentClassId = classid;
        this.currentNewsId = newsid;
        this.getNewsDetails(newsid);
      },
    },
    mounted() {
      this.$nextTick(function () {
        let img = this.$refs.imgH;
        img.onload = function () {
          document.getElementsByClassName("xw")[0].style.top = -img.offsetHeight / 2 + 'px'
        }
      })
      this.getClassList();
    }
  };

</script>

<style scoped>
  .bk {
    background-color: #000;
    /* min-height: 1200px; */
  }

  .title_h1 {
    width: 100%;
  }

  .xw {
    position: relative;
  }

  .newsblock {
    width: 1200px;
    height: 400px;
    margin: 0 auto;
    margin-top: 50px;
    background-color: rgba(30, 30, 30, 0.8);
    border-radius: 20px;
    padding: 44px;
  }

  .classtitle {
    width: 558px;
    float: left;
    height: 322px;
  }

  .articleblock {
    width: 525px;
    height: 322px;
    float: left;
    overflow-y: scroll;
  }

  .articleblock::-webkit-scrollbar {
    width: 10px;
    margin-left: 5px;
  }

  .articleblock::-webkit-scrollbar-button {
    display: none;
  }

  .articleblock::-webkit-scrollbar-thumb {
    background-color: #000;
    border-radius: 5px;
  }

  .articleblock ul {
    padding: 0px;
  }

  .classtitle-item1 {
    float: left;
  }

  ul li {
    list-style-type: none;
  }

  .articleblock ul li {
    color: #ccc;
    line-height: 50px;
    font-size: 14px;
    border-bottom: 1px solid #272727;
    height: 50px;
    position: relative;
    cursor: pointer;
  }

  .articledot {
    width: 2px;
    height: 2px;
    background-color: #ef782d;
    float: left;
    margin: 24px 10px 20px 3px;
  }

  .articlename {
    float: left;
  }

  .articletime {
    float: right;
    margin-right: 8px;
  }

  .tdimg {
    margin-left: 48px;
    float: left;
  }

  .tdtitle {
    float: left;
    margin-left: 10px;
    cursor: pointer;
  }

  .newsdetailsblock {
    margin: auto;
    width: 75vw;
    height: auto;
    background-color: rgba(30, 30, 30, 0.8);
    border-radius: 20px;
    position: relative;
  }

  .leftnav {
    width: 15vw;
    height: 100%;
    display: inline-block;
    vertical-align: top;
  }

  .rightdtals {
    display: inline-block;
    width: calc(60vw - 5px);
    vertical-align: top;
    padding: 20px;
    padding-bottom: 50px;
    border-left: 1px solid #313131;
  }

  .leftNav-div {
    position: absolute;
    top: 20px;
  }

  .newsdetails-item {
    width: 100%;
    display: inline-block;
  }

  .newsdetails-classtitle {
    border-bottom: 1px solid #313131;
    padding: 15px 0px 10px 15px;
  }

  .newsdetails-classtitle-item1 {
    float: left;
    width: 20px;
    margin-right: 8px;
    margin-top: 11px;
  }

  .newsdetails-classtitle-item2 {
    line-height: 40px;
    color: #cbcbcb;
    font-size: 18px;
  }

  .leftnav ul {
    padding: 0px;
    color: #acacac;
  }

  .leftnav ul li {
    padding: 8px 0px 6px 30px;
    border-bottom: 1px solid #313131;
    cursor: pointer;
    line-height: 22px;
  }

  .activenews {
    color: #ef782c;
  }

  .newsdot {
    width: 3px;
    height: 3px;
    float: left;
    background-color: #acacac;
    margin-top: 9px;
    margin-right: 6px;
  }

  .newsdetails-title {
    height: 35px;
    font-size: 26px;
    font-family: PingFang SC;
    font-weight: bold;
    color: #fff;
    text-align: center;
  }

  .newsdetails-content {
    padding-top: 20px;
    font-size: 14px;
    font-family: PingFang SC;
    font-weight: 400;
    line-height: 24px;
    color: #393939;
  }




  @media (max-width: 768px) {
    .newsdetailsblock {
      margin: auto 5px;
    }

    .leftnav {
      display: none;
    }

    .newsdetails-content p {
      background-color: aquamarine;
    }

    .newsdetails-content p img {
      width: 100%;
    }
  }

  @media (max-width: 768px) {
    .newsblock {
      width: 100%;
      margin: 0 auto;
      margin-top: 50px;
      border: 1px solid #DDDDDD;
    }
  }

  @media (max-width: 980px) {
    .newsblock {
      width: 90%;
      margin: 0 auto;
      margin-top: 50px;
      border: 1px solid #DDDDDD;
    }
  }

</style>

<style>
  @media (max-width: 768px) {

    .newsdetails-content p img {
      width: 100%;
    }

    .rightdetails {
      width: 100%;
    }

    .tdimg {
      margin-left: 8px !important;

    }
  }

  .newsdetails-content img {
    max-width: calc(60vw - 40px);
  }

  .newsdetails-content h3 a {
    overflow: hidden;
    width: 100%;
    display: block;
    text-overflow: ellipsis;

  }

</style>
